package org.example.homework.mapper;

import org.apache.ibatis.annotations.*;
import org.example.homework.entity.CartItem;
import java.util.List;

@Mapper
public interface CartMapper {
    @Insert("INSERT INTO cart_item (user_id, product_id, quantity) VALUES (#{userId}, #{productId}, #{quantity})")
    void insert(CartItem cartItem);

    @Select("SELECT * FROM cart_item WHERE user_id = #{userId}")
    @Results({
        @Result(property = "id", column = "id"),
        @Result(property = "userId", column = "user_id"),
        @Result(property = "productId", column = "product_id"),
        @Result(property = "quantity", column = "quantity"),
        @Result(property = "product", column = "product_id",
            one = @One(select = "org.example.homework.mapper.ProductMapper.findById"))
    })
    List<CartItem> findByUserId(Integer userId);

    @Select("SELECT * FROM cart_item WHERE user_id = #{userId} AND product_id = #{productId}")
    CartItem findByUserIdAndProductId(@Param("userId") Integer userId, @Param("productId") Integer productId);

    @Delete("DELETE FROM cart_item WHERE id = #{itemId} AND user_id = #{userId}")
    void deleteByIdAndUserId(@Param("itemId") Integer itemId, @Param("userId") Integer userId);

    @Update("UPDATE cart_item SET quantity = #{quantity} WHERE id = #{itemId}")
    void updateQuantity(@Param("itemId") Integer itemId, @Param("quantity") Integer quantity);
} 